package com.machine.filling.function.main.PayRes;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.main.topFg.TopFgFragment;
import com.machine.filling.util.Common;

/**
 * Created by Administrator on 2018/4/1 0001.
 *
 * @description
 */
@SuppressLint("ValidFragment")
public class PayResFragment extends BaseFragment<PayResContract.Presenter> implements PayResContract.View, View.OnClickListener {

    private INextFragment INextFragment;
    private TextView tv_pay_type;
    private TextView tv_money;
    private TextView tv_back_time;
    private ImageView iv_back;

    private String payType;
    private String payMoney;

    private long BACK_TIME = Common.BACK_TIME;//返回主界面的倒计时s
    private LinearLayout gun_ll;

    /**
     * //1支付宝 2微信 3现金 4油卡 5预约加油
     *
     * @param payType
     * @param payMoney
     */
    public PayResFragment(String payType, String payMoney) {
        this.payType = payType;
        this.payMoney = payMoney;
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_pay_res, null);
        iv_back = view.findViewById(R.id.iv_back);
        tv_pay_type = view.findViewById(R.id.tv_pay_type);
        tv_back_time = view.findViewById(R.id.tv_back_time);
        gun_ll = view.findViewById(R.id.gun_ll);
        tv_money = view.findViewById(R.id.tv_money);
        iv_back.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        if (payMoney.equals("")) {
            gun_ll.setVisibility(View.GONE);
        }
        tv_money.setText(payMoney + "元");
        switch (payType) {//1支付宝 2微信 3现金 4油卡
            case Common.PAY_MODE_ZHIFUBAO:
                tv_pay_type.setText("支付宝支付");
                break;
            case Common.PAY_MODE_WEIXIN:
                tv_pay_type.setText("微信支付");
                break;
            case Common.PAY_MODE_CASH:
                tv_pay_type.setText("现金支付");
                break;
            case Common.PAY_MODE_CARD:
                tv_pay_type.setText("油卡支付");
                break;
            case Common.PAY_MODE_ORDER:
                tv_pay_type.setText("预约加油");
                break;
        }
        BACK_TIME = Common.BACK_TIME;
        tv_back_time.setText(BACK_TIME + "s");
        handler.sendEmptyMessageDelayed(1001, 1 * 1000);//每隔1秒进行一次发消息更新界面
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
                break;
        }
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1001:
                    BACK_TIME--;
                    tv_back_time.setText(BACK_TIME + "s");
                    if (BACK_TIME <= 0) {
                        INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
                    } else {
                        handler.sendEmptyMessageDelayed(1001, 1 * 1000);
                    }
                    break;
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        handler.removeMessages(1001);//取消之前所有循环
    }

}
